﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NExt.Handler.Component.Default.JsSerialize
{
    internal class CompositeNExtScriptGenerator: INExtScriptGenerator
    {
        private const string COMPOSITE_SCRIPT_SEPERATOR = "\r\n";
        private readonly IEnumerable<INExtScriptGenerator> scriptGenerators;

        public CompositeNExtScriptGenerator(params INExtScriptGenerator[] scriptGenerators)
        {
            this.scriptGenerators = scriptGenerators;
        }

        public string Build(Type type)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (INExtScriptGenerator scriptGenerator in scriptGenerators)
            {
                string script = scriptGenerator.Build(type);
                stringBuilder.Append(COMPOSITE_SCRIPT_SEPERATOR);
                script = script.Trim();
                if (string.IsNullOrEmpty(script))
                    continue;

                stringBuilder.Append(script);
            }
            return stringBuilder.ToString();
        }
    }
}
